package vn.co.thn.android.videohot.activity;

import java.util.ArrayList;

import vn.co.thn.android.serviceconnect.callback.RequestCallBack;
import vn.co.thn.android.serviceconnect.request.Param;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.VHUrlApi;
import vn.co.thn.android.videohot.common.VHCommonYouTubeActivity;
import vn.co.thn.android.videohot.data.VHVideoItemData;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

public class VHVideoDetailActivity extends VHCommonYouTubeActivity implements OnClickListener {
	private VHVideoItemData video;
	private TextView title, content;
	private InterstitialAd mInterstitial;
	YouTubePlayerFragment youTubePlayerFragment;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		overridePendingTransition(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit);
		setContentView(R.layout.vh_video_detail_activity);
		findViewById(R.id.btn_fb_share).setOnClickListener(this);
		youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
		title = (TextView) findViewById(R.id.title);
		content = (TextView) findViewById(R.id.content);
		if (getIntent().getDataString() == null) {
			video = (VHVideoItemData) getIntent().getSerializableExtra(VHConstants.EXTRA_VIDEO);

			title.setText(video.getTitle());
			content.setText(video.getContent().getValue());

			youTubePlayerFragment.initialize(VHConstants.API_KEY, this);
			setView(video.getEmbed_code());

		} else {
			String key = getIntent().getData().getQueryParameter("key");
			LoadData(key);
			setView(key);
		}

		findViewById(R.id.nav_back).setOnClickListener(this);
		// ad
		mInterstitial = new InterstitialAd(this);
		mInterstitial.setAdUnitId(getResources().getString(R.string.admod_id));
		mInterstitial.setAdListener(new AdListener() {
			@Override
			public void onAdOpened() {
				// TODO Auto-generated method stub
				super.onAdOpened();
				
			}

			@Override
			public void onAdLoaded() {
				// TODO Auto-generated method stub
				super.onAdLoaded();
				if (mInterstitial.isLoaded()) {
					mInterstitial.show();
				} else {
					mInterstitial.show();
				}
			}
		});
		mInterstitial.loadAd(new AdRequest.Builder().build());
	}

	private void LoadData(String key) {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("Key", key));
		this.mRequestService.setUrl(VHUrlApi.Api_Video_Item);
		this.mRequestService.setParams(params);
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHVideoItemData>() {

			@Override
			public void onResult(VHVideoItemData data, boolean isUpdate) {
				// TODO Auto-generated method stub
				video = data;
				title.setText(video.getTitle());
				content.setText(video.getContent().getValue());
				youTubePlayerFragment.initialize(VHConstants.API_KEY, VHVideoDetailActivity.this);
			}

			@Override
			public void onResult(ArrayList<VHVideoItemData> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		mRequestService.setRequestError(this);
		this.mRequestService.Post();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}

	private void setView(String key) {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("Key", key));
		this.mRequestService.setUrl(VHUrlApi.Api_Video_set_review);
		this.mRequestService.setParams(params);
		// this.mRequestService.setObjectResult(VHVideoItemData.class);
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHVideoItemData>() {

			@Override
			public void onResult(VHVideoItemData data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResult(ArrayList<VHVideoItemData> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		mRequestService.setRequestError(this);
		this.mRequestService.Post();

	}

	@Override
	public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
		if (!wasRestored) {
			player.cueVideo(video.getEmbed_code());
		}
	}

	@Override
	protected YouTubePlayer.Provider getYouTubePlayerProvider() {
		return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
	}

	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_right_exit);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nav_back:
			finish();
			overridePendingTransition(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_right_exit);
			break;
		case R.id.btn_fb_share:
			startActivityForResult(new Intent(getApplicationContext(), VHAuthFacebookActivity.class).putExtra(VHConstants.EXTRA_VIDEO, video),
					VHConstants.REQUEST_CODE_AUTH_FACEBOOK);
			break;
		default:
			break;
		}
	}

	@Override
	public void onError(VolleyError error) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onException(Exception e) {
		// TODO Auto-generated method stub

	}

}
